<template>
  <div class="el_card_main">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form
          ref="formSelect"
          :model="formSelect"
          label-width="80px"
          :inline="true"
        >
          <el-row :gutter="12">
            <el-col :span="6">
              <el-form-item label="标签名称">
                <el-input
                  placeholder="请输入"
                  v-model="formSelect.tags"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-select
                  v-model="formBase.province"
                  @change="getCityList()"
                  style="width: 290px"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in formSelect.province"
                    :key="index"
                    :label="item"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="地区">
                <el-select
                  v-model="formBase.city"
                  placeholder="请选择"
                  style="width: 290px"
                >
                  <el-option
                    v-for="(item, index) in formSelect.city"
                    :key="index"
                    :label="item"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input
                  placeholder="请选择"
                  v-model="formSelect.shortName"
                  clearable
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态">
                <el-select
                  v-model="formSelect.state"
                  style="width: 290px"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in status"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item style="padding-left: 16px">
                <el-button style="padding: 3px 0" @click="clearContent"
                  >清空</el-button
                >
                <el-button style="padding: 3px 0" type="primary"
                  >搜索</el-button
                >
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="float: right; padding-right: 12px">
                <el-button icon="el-icon-edit" type="success" @click="add"
                  >新增用户</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <el-alert :title="title" type="info" show-icon :closable="closable">
      </el-alert>
      <!-- 企业列表 -->
      <div class="text item">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            prop="id"
            label="序号"
            width="180"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="number"
            label="企业编号"
            width="180"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="company"
            label="企业简称"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="tags"
            label="标签"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="creatorID"
            label="创建者"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="addDate"
            label="创建日期"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="remarks"
            label="备注"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="state"
            label="状态"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column label="操作" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                plain
                circle
                style="width: 36px; height: 36px"
                @click="handleEdit(scope.row)"
              ></el-button>
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-close"
                plain
                circle
                style="width: 36px; height: 36px"
                @click="handleBan(scope.row)"
              ></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                plain
                circle
                style="width: 36px; height: 36px"
                @click="handleDelete(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 放置分页组件 -->
        <el-row type="flex" style="height: 60px" align="middle" justify="end">
          <!-- 放置分页组件 -->
          <el-pagination
            layout="prev, pager, next, sizes,jumper"
            :total="total"
            :current-page="queryObj.page"
            :page-sizes="[5, 10, 20, 40]"
            :page-size="queryObj.pagesize"
            @size-change="handleSizeChange"
            @current-change="handlePageChange"
          />
        </el-row>
      </div>
      <!-- 新增企业表 -->
      <CompanysAdd
        :titleInfo="titleInfo"
        :formBase="formBase"
        :optionType.sync="optionType"
        ref="companys"
        :id="id"
        @getlist="renderTable"
      />
    </el-card>
  </div>
</template>

<script>
import { provinces, citys } from '@/api/hmmm/citys.js'
import { list, remove, update, detail, disabled } from '@/api/hmmm/companys.js'
import CompanysAdd from '../components/companys-add.vue'
export default {
  components: {
    CompanysAdd
  },
  data () {
    return {
      input: '',
      value: false,
      centerDialogVisible: false,
      dialogVisible: false,
      dialogFormVisible: false,
      closable: false,
      optionType: false,
      cityList: [],
      total: 0,
      titleInfo: {
        text: '创建用户'
      },
      id: null,
      // 筛选表单
      formSelect: {
        tags: '',
        province: [],
        city: [],
        shortName: '',
        state: ''
      },
      // 新增用户表单
      formBase: {
        shortName: '',
        isFamous: false,
        company: '',
        province: '',
        tags: '',
        remarks: '',
        city: ''
      },
      status: [
        {
          value: 1,
          label: '启用'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        email: [{ required: true, message: '邮箱不能为空', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }
        ]
      },
      queryObj: {
        page: 1,
        pagesize: 10
      },
      tableData: []
    }
  },

  async created () {
    this.renderTable()
    this.getProvinceList()
  },

  methods: {
    // 清空事件
    clearContent () {
      this.input = ''
    },
    // 渲染表单
    async renderTable () {
      const { data } = await list(this.queryObj)
      console.log(data)
      this.total = data.counts
      this.tableData = [...data.items]
    },
    // 编辑成员
    async handleEdit (row) {
      this.$refs.companys.dialogFormVisible = true
      await detail(row.id)
      // TODO
    },
    // 删除成员
    async handleDelete (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await remove(row.id)
          console.log(res)

          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.renderTable()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 获取城市1级
    async getProvinceList () {
      const data = await provinces()
      this.formSelect.province = [...data]
    },
    // 获取城市2级
    async getCityList () {
      const data = await citys(this.formBase.province)
      this.formSelect.city = [...data]
    },
    // BAN
    handleBan (row) {
      this.$confirm('已成功禁用, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await disabled(row.id)
          console.log(res)

          this.$message({
            type: 'success',
            message: '已成功禁用!'
          })
          this.renderTable()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作!'
          })
        })
    },
    // 更新成员
    async updateForm () {
      const res = await update(this.form)
      console.log(res)
      this.closeDialog()
    },
    // 关闭弹窗,并重置表单
    closeDialog () {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    add () {
      this.$refs.companys.dialogFormVisible = true
      this.optionType = true
    },
    // 页数变化时触发
    handlePageChange (pageNum) {
      this.queryObj.page = pageNum
      this.renderTable()
    },
    // 每页显示信息条数
    handleSizeChange (pageSize) {
      this.queryObj.pagesize = pageSize
      this.renderTable()
    }
  },
  computed: {
    // 消息记录
    title () {
      return `共 ${this.total} 条记录`
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  width: 1681px;
  margin: 0 auto;
  padding: 20px;
  margin-top: 20px;
  .button_box {
    display: flex;
    width: 220px;
  }
  .el-input {
    width: 290px;
    height: 32px;
    padding: 0;
  }
  .addForm {
    .el-input {
      width: 280px;
      height: 36px;
    }
  }
  .el-button--default {
    width: 56px;
    height: 32px;
    margin: 0 0 0 15px;
  }
  .el-alert--info {
    margin-bottom: 20px;
  }
  .el-button--primary {
    width: 56px;
    height: 32px;
  }
  .el-button--success {
    width: 97px;
    height: 32px;
    font-size: 12px;
  }
  .text {
    font-size: 14px;
  }
  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: '';
  }
  .clearfix:after {
    clear: both;
  }
}
</style>
